package com.lswms.modules.lswms.service.impl.base;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.dao.base.CustomerDao;
import com.lswms.modules.lswms.dto.base.CustomerDTO;
import com.lswms.modules.lswms.entity.base.CustomerEntity;
import com.lswms.modules.lswms.service.base.CustomerService;

/**
 * 公司 
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-30
 */
@Service
public class CustomerServiceImpl extends CrudServiceImpl<CustomerDao, CustomerEntity, CustomerDTO> implements CustomerService {

    @Override
    public QueryWrapper<CustomerEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String companyid = (String)params.get("companyid");
        String companyname = (String)params.get("companyname");

        QueryWrapper<CustomerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(companyid), "companyid", companyid);
        wrapper.like(StringUtils.isNotBlank(companyname), "companyname", companyname);

        wrapper.orderByAsc("companyname");
        return wrapper;
    }

	@Override
	public String valideAndSave(CustomerDTO dto) {
		String companyId = dto.getCompanyid() ;
		if(StringUtils.isBlank(companyId)) {
			return "来往关系id不能为空，请填写" ;
		}
		companyId = companyId.trim();
		
		QueryWrapper<CustomerEntity> queryCustomer = new QueryWrapper<CustomerEntity>();
		queryCustomer.eq("companyId", companyId);
		Integer count = baseDao.selectCount(queryCustomer) ;
		
		if(count>0) {
			return "来往关系id已存在，请重新填写" ;
		}
		CustomerEntity customerEntity = new CustomerEntity();
		customerEntity.setCompanyid(companyId);
		customerEntity.setCompanyname(StringUtils.trim(dto.getCompanyname()) );
		customerEntity.setCustomer(StringUtils.trim(dto.getCustomer()) );
		customerEntity.setPhone(StringUtils.trim(dto.getPhone()) );
		customerEntity.setAddress(StringUtils.trim(dto.getAddress()));
		
		baseDao.insert(customerEntity);
		
		return null;
	}

	@Override
	public String valideAndUpdate(CustomerDTO dto) {
		String companyId = dto.getCompanyid() ;
		if(StringUtils.isBlank(companyId)) {
			return "来往关系id不能为空，请填写" ;
		}
		companyId = companyId.trim();
		
		CustomerEntity customerEntity = new CustomerEntity();
//		customerEntity.setCompanyid(companyId);
		customerEntity.setCompanyname(StringUtils.trim(dto.getCompanyname()) );
		customerEntity.setCustomer(StringUtils.trim(dto.getCustomer()) );
		customerEntity.setPhone(StringUtils.trim(dto.getPhone()) );
		customerEntity.setAddress(StringUtils.trim(dto.getAddress()));
		
		UpdateWrapper<CustomerEntity> updateWrapper = new UpdateWrapper<CustomerEntity>();
		updateWrapper.eq("companyId", companyId);
		
		baseDao.update(customerEntity , updateWrapper);
		
		return null;
	}


}